#include "Sem.hpp"

int main(){
    int cnt = 3;
    BulidSemphore builder(CREATE_SEM);
    auto fsem = builder.bulid();
    builder.Init(); 

    
    auto csem = BulidSemphore(GET_SEM).bulid();
    pid_t subid = fork();

    if(subid < 0){
        cerr << "fork error!" << endl;
        exit(errno);
    }
    else if(subid == 0){
        while(cnt--){
            csem->P();
            printf("%c", 'C');
            fflush(stdout);
            printf("%c", 'C');
            fflush(stdout);
            sleep(1);
            csem->V();
        }
        cout << endl;
        exit(0);
    }
    else{
        while(cnt--){
            fsem->P();
            printf("%c", 'F');
            fflush(stdout);
            printf("%c", 'F');
            fflush(stdout);
            sleep(1);
            fsem->V();
        };
        
        waitpid(subid, nullptr, 0);
    }
    return 0;
}